using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectRestaurant()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectRestaurantByIDDistrict(int iddistrict)
		{
			var data = new DataTransferViewModel
			{
				IDDistrict = iddistrict,
				EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT,
			};
			return View("SelectRestaurant", CreateViewModel(data));
		}

		public ActionResult SelectRestaurantByIDDistrictPaging(int iddistrict)
		{
			var data = new DataTransferViewModel
			{
				IDDistrict = iddistrict,
				EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT,
			};
			return PartialView("Templates/TH_ListRestaurant", CreateViewModel(data));
		}

		public ActionResult SelectRestaurantByIDFoodType(int idfoodtype)
		{
			var data = new DataTransferViewModel
			{
				IDFoodType = idfoodtype,
				EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT,
			};
			return View("SelectRestaurant", CreateViewModel(data));
		}

        public ActionResult SelectRestaurantByIDDistrictAndIDFoodType(int iddistrict, int idfoodtype)
        {
            var data = new DataTransferViewModel
            {
                IDDistrict = iddistrict,
                IDFoodType = idfoodtype,
                EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT,
            };
            return View("SelectRestaurant", CreateViewModel(data));
        }

		public ActionResult SelectRestaurantByIDFoodTypePaging(int idfoodtype)
		{
			var data = new DataTransferViewModel
			{
				IDFoodType = idfoodtype,
				EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT,
			};
			return PartialView("Templates/TH_ListRestaurant", CreateViewModel(data));
		}

        public ActionResult DeleteRestaurant(int id, int page = 1)
		{
			var deleteItem = _repRestaurant.SelectByID(id);
			var iddistrict = deleteItem.IDDistrict;
			var idfoodtype = deleteItem.IDFoodType;
			var result = _repRestaurant.Delete(id);
            var data = new DataTransferViewModel
            {
                InfoText = result ? "Item has been deleted" : "Cannot delete item!",
                EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT,
                CurrentPage = page,
                IDDistrict = iddistrict,
                IDFoodType = idfoodtype
            };
            return View("SelectRestaurant", CreateViewModel(data));
		}

		public ActionResult DetailOfRestaurant(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_RESTAURANT,
				IDRestaurant = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateRestaurant(FormCollection forms)
		{
			string id = forms.GetValues("Restaurant_ID")[0];
			string iddistrict = forms.GetValues("Restaurant_IDDistrict")[0];
			string idfoodtype = forms.GetValues("Restaurant_IDFoodType")[0];
			string name = forms.GetValues("Restaurant_Name")[0];
			string estimatedservicetime = forms.GetValues("Restaurant_EstimatedServiceTime")[0];
			string deliverydistricts = forms.GetValues("Restaurant_DeliveryDistricts")[0];
			string timestart = forms.GetValues("Restaurant_TimeStart")[0];
			string timeend = forms.GetValues("Restaurant_TimeEnd")[0];
			string address = forms.GetValues("Restaurant_Address")[0];
			var file = Request.Files["Restaurant_Image"];

			var editItem = _repRestaurant.SelectByID(int.Parse(id));
			editItem.Name = name;
			editItem.EstimatedServiceTime = int.Parse(estimatedservicetime.Replace(".", "").Replace(",", ""));
			editItem.DeliveryDistricts = deliverydistricts;
			editItem.TimeStart = timestart;
			editItem.TimeEnd = timeend;
			editItem.Address = address;
			var result = false;

			if (file.ContentLength != 0)
			{
                byte[] image = null;
                string refName = string.Empty;
                result = UploadFile(file, id, ref image, ref refName);
                if (result)
                {
                    DALService.Instance.UploadFile(refName, image, editItem.Image);
                    editItem.Image = refName;
                    editItem.ContentType = file.ContentType;
                }
			}

			result = _repRestaurant.Update(editItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_RESTAURANT,
				IDRestaurant = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfRestaurant", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertRestaurant(FormCollection forms)
		{
			string iddistrict = forms.GetValues("Restaurant_IDDistrict")[0];
			string idfoodtype = forms.GetValues("Restaurant_IDFoodType")[0];
			string name = forms.GetValues("Restaurant_Name")[0];
			string estimatedservicetime = forms.GetValues("Restaurant_EstimatedServiceTime")[0];
			string deliverydistricts = forms.GetValues("Restaurant_DeliveryDistricts")[0];
			string timestart = forms.GetValues("Restaurant_TimeStart")[0];
			string timeend = forms.GetValues("Restaurant_TimeEnd")[0];
			string address = forms.GetValues("Restaurant_Address")[0];
			var file = Request.Files["Restaurant_Image"];
			var newItem = new RestaurantDTO
			{
				IDDistrict = int.Parse(iddistrict),
				IDFoodType = int.Parse(idfoodtype),
				Name = name,
				DeliveryDistricts = deliverydistricts,
				TimeStart = timestart,
				TimeEnd = timeend,
				Address = address,
                Image = "noimages.png",
                ContentType = "image/x-png"
			};
            if (estimatedservicetime != "")
            {
                newItem.EstimatedServiceTime = int.Parse(estimatedservicetime);
            }
            var result = _repRestaurant.Insert(ref newItem);
			if (result)
			{
                if (file.ContentLength != 0)
                {
                    byte[] image = null;
                    string refName = string.Empty;
                    result = UploadFile(file, newItem.ID.ToString(), ref image, ref refName);
                    if (result)
                    {
                        var addedItem = _repRestaurant.SelectByID(newItem.ID);
                        if (addedItem != null)
                        {
                            // Upload image to service
                            DALService.Instance.UploadFile(refName, image, string.Empty);
                            addedItem.Image = refName;
                            addedItem.ContentType = file.ContentType;
                            result = _repRestaurant.Update(addedItem);
                        }
                    }
                }
			}
			var data = new DataTransferViewModel
			{
				IDDistrict = int.Parse(iddistrict),
				IDFoodType = int.Parse(idfoodtype),
				EnumViewModelType = EnumViewModel.ADMIN_RESTAURANT,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectRestaurant", CreateViewModel(data));
		}


	}
}